﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Web.Syndication;

namespace Company.Logger.Logs
{
    public class Logger
    {
        public ObservableCollection<Log> Logs { get; set; }
        public Logger()
        {
            Logs = new ObservableCollection<Log>();
        }
        /// <summary>
        /// 清除日志缓存内存
        /// </summary>
        public void Clear()
        {
            Logs.Clear();
        }
        public bool SaveToFile(string filePath)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(filePath,true))
                {
                    foreach (Log log in Logs)
                    {
                        // 写入日志时间戳和消息
                        writer.WriteLine($"[{log.LogTime.ToString("yyyy.MM.dd HH:mm:ss")}]-{log.Level}-{log.Message}");
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                // 处理文件保存过程中可能出现的异常
                Console.WriteLine($"保存日志到文件时出错: {ex.Message}");
                return false;
            }
        }
    }
}
